package pt.isec.gps1415.g15.drinkanddrive;

import java.util.Calendar;

import android.app.Activity;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TimePicker;

public class EcraPrincipal extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_ecra_principal);
	}

	public void btnRegistarConsumo(View v) {
		Intent intent = new Intent(this, MainListaBebidas.class);
		startActivity(intent);

	}

	public void btnHoraVoltarConduzir(View v) {
		DialogFragment newFragment = new TimePickerFragment();
	    newFragment.show(getFragmentManager(), "timePicker");
	}
}

class TimePickerFragment extends DialogFragment implements
		
TimePickerDialog.OnTimeSetListener {

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		// Use the current time as the default values for the picker
		final Calendar c = Calendar.getInstance();
		int hour = c.get(Calendar.HOUR_OF_DAY);
		int minute = c.get(Calendar.MINUTE);
		// Create a new instance of TimePickerDialog and return it
		TimePickerDialog tpd= new TimePickerDialog(getActivity(), this, hour, minute,
				DateFormat.is24HourFormat(getActivity()));
		tpd.setTitle("Registar Hora Para Voltar a Conduzir");
		return tpd;
	}

	public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
		Log.i("Hora", hourOfDay + ":" + minute);
		SharedPreferences pref = getActivity().getSharedPreferences("HORACONDUZIR",getActivity().MODE_MULTI_PROCESS);
		Editor editor = pref.edit();
		editor.putInt("HORA", hourOfDay);
		editor.putInt("MINUTOS", minute);
		editor.apply();
	}
}
